package com.utn.tp.integrador.widget;

import com.utn.tp.integrador.R;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class ProveedorWidget extends AppWidgetProvider {

	ControladorWidget controlador;
	public static String AVION = "AVION";
	public static String DATOS = "DATOS";
	public static String BLUETOOTH = "BLUETOOTH";
	public static String WIFI = "WIFI";
	public static String BRILLO = "BRILLO";
	public static String SONIDO = "SONIDO";

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		ComponentName thisWidget = new ComponentName(context,
				ProveedorWidget.class);
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.widgetlayout);
		final int N = appWidgetIds.length;
		for (int i = 0; i < N; i++) {
			addClickListeners(remoteViews, context);
			appWidgetManager.updateAppWidget(thisWidget, remoteViews);
		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		controlador = new ControladorWidget(context);
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.widgetlayout);
		if (intent.getAction().equals(AVION)) {
			controlador.modoAvion();
		} else if (intent.getAction().equals(BRILLO)) {
			context.startActivity(new Intent(context, Brillo.class)
					.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
		} else if (intent.getAction().equals(WIFI)) {
			controlador.wifi(new Dummy());
		} else if (intent.getAction().equals(DATOS)) {
			controlador.TresG(false);
		} else if (intent.getAction().equals(BLUETOOTH)) {
			controlador.bluetooth();
		} else if (intent.getAction().equals(SONIDO)) {
			controlador.audio();
		}
		ComponentName cn = new ComponentName(context, AppWidgetProvider.class);
		AppWidgetManager.getInstance(context).updateAppWidget(cn, remoteViews);
	}

	protected void addClickListeners(RemoteViews remoteViews, Context context) {
		Intent intent = new Intent(context, ProveedorWidget.class);
		intent.setAction(AVION);
		PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,
				0, intent, 0);
		remoteViews.setOnClickPendingIntent(R.id.btnAvion, actionPendingIntent);
		intent.setAction(BRILLO);
		actionPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		remoteViews
				.setOnClickPendingIntent(R.id.btnBrillo, actionPendingIntent);
		intent.setAction(WIFI);
		actionPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		remoteViews.setOnClickPendingIntent(R.id.btnWiFi, actionPendingIntent);
		intent.setAction(DATOS);
		actionPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		remoteViews.setOnClickPendingIntent(R.id.btnDatos, actionPendingIntent);
		intent.setAction(BLUETOOTH);
		actionPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		remoteViews.setOnClickPendingIntent(R.id.btnBluetooth,
				actionPendingIntent);
		intent.setAction(SONIDO);
		actionPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		remoteViews.setOnClickPendingIntent(R.id.btnSonido,
				actionPendingIntent);

	}

}
